<template>
  <div class="repayment-record">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>还款记录</h3>
          <div class="header-actions">
            <el-button type="success" @click="handleExport">导出</el-button>
          </div>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :model="searchForm" inline class="search-form">
        <el-form-item label="贷款编号">
          <el-input v-model="searchForm.loanNo" placeholder="请输入贷款编号" clearable></el-input>
        </el-form-item>
        <el-form-item label="客户姓名">
          <el-input v-model="searchForm.customerName" placeholder="请输入客户姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="还款方式">
          <el-select v-model="searchForm.payMethod" placeholder="请选择还款方式" clearable>
            <el-option label="银行转账" value="1"></el-option>
            <el-option label="现金" value="2"></el-option>
            <el-option label="支付宝" value="3"></el-option>
            <el-option label="微信支付" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="还款日期">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="record_id" label="记录ID" width="100"></el-table-column>
        <el-table-column prop="contract_id" label="合同ID" width="100"></el-table-column>
        <el-table-column prop="transaction_no" label="交易流水号" width="180"></el-table-column>
        <el-table-column prop="principal" label="本金" width="120">
          <template #default="scope">
            {{ formatMoney(scope.row.principal) }}
          </template>
        </el-table-column>
        <el-table-column prop="interest" label="利息" width="120">
          <template #default="scope">
            {{ formatMoney(scope.row.interest) }}
          </template>
        </el-table-column>
        <el-table-column prop="penalty" label="罚息" width="120">
          <template #default="scope">
            {{ formatMoney(scope.row.penalty) }}
          </template>
        </el-table-column>
        <el-table-column prop="repay_amount" label="还款金额" width="120">
          <template #default="scope">
            {{ formatMoney(scope.row.repay_amount) }}
          </template>
        </el-table-column>
        <el-table-column prop="pay_method_name" label="还款方式" width="120"></el-table-column>
        <el-table-column prop="repay_time" label="还款时间" width="180"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleDetail(scope.row)">详情</el-button>
            <el-button link type="primary" @click="handlePrint(scope.row)">打印</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="还款记录详情" width="700px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="记录ID">{{ currentRecord.record_id }}</el-descriptions-item>
        <el-descriptions-item label="合同ID">{{ currentRecord.contract_id }}</el-descriptions-item>
        <el-descriptions-item label="计划ID">{{ currentRecord.plan_id }}</el-descriptions-item>
        <el-descriptions-item label="交易流水号">{{ currentRecord.transaction_no }}</el-descriptions-item>
        <el-descriptions-item label="本金">{{ formatMoney(currentRecord.principal) }}</el-descriptions-item>
        <el-descriptions-item label="利息">{{ formatMoney(currentRecord.interest) }}</el-descriptions-item>
        <el-descriptions-item label="罚息">{{ formatMoney(currentRecord.penalty) }}</el-descriptions-item>
        <el-descriptions-item label="还款金额">{{ formatMoney(currentRecord.repay_amount) }}</el-descriptions-item>
        <el-descriptions-item label="还款方式">{{ currentRecord.pay_method_name }}</el-descriptions-item>
        <el-descriptions-item label="还款时间">{{ currentRecord.repay_time }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentRecord.create_time }}</el-descriptions-item>
        <el-descriptions-item label="是否删除">{{ currentRecord.is_deleted === 0 ? '否' : '是' }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { repaymentRecordApi } from '@/api/repayment'

// 表格数据
const loading = ref(false)
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 搜索表单
const searchForm = reactive({
  loanNo: '',
  contractNo: '',
  customerName: '',
  payMethod: '',
  dateRange: []
})

// 详情对话框
const detailDialogVisible = ref(false)
const currentRecord = ref({})

// 初始化
onMounted(() => {
  getTableData()
})

// 获取表格数据
const getTableData = () => {
  loading.value = true
  
  const params = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    loanNo: searchForm.loanNo,
    contractNo: searchForm.contractNo,
    customerName: searchForm.customerName,
    payMethod: searchForm.payMethod,
    startDate: searchForm.dateRange && searchForm.dateRange.length > 0 ? searchForm.dateRange[0] : '',
    endDate: searchForm.dateRange && searchForm.dateRange.length > 0 ? searchForm.dateRange[1] : ''
  }
  
  repaymentRecordApi.getList(params).then(res => {
    tableData.value = res.data.records
    total.value = res.data.total
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 查询
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = key === 'dateRange' ? [] : ''
  })
  handleSearch()
}

// 查看详情
const handleDetail = (row) => {
  currentRecord.value = row
  detailDialogVisible.value = true
}

// 导出
const handleExport = () => {
  ElMessage.success('导出功能待开发')
}

// 打印
const handlePrint = (row) => {
  ElMessage.success('打印功能待开发')
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}

// 格式化金额
const formatMoney = (value) => {
  return `¥ ${parseFloat(value).toFixed(2)}`
}

// 获取还款方式文本
const getPayMethodText = (payMethod) => {
  const map = {
    '1': '银行转账',
    '2': '现金',
    '3': '支付宝',
    '4': '微信支付'
  }
  return map[payMethod] || '未知'
}
</script>

<style scoped>
.repayment-record {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 